package com.colabo.j2ee.web.core.license;

import com.colabo.j2ee.web.core.log.DaemonLogger;

/**
 * Factory for producing license implementation
 * @author Anthony Do
 * @since 2009-12-23
 *
 */
public class LicenseFactory {

	private static LicenseFactory instance = null;
	private static ILicense DEFAULT_LICENSE = new NullLicense();
	private LicenseFactory() {
	}
	
	public static synchronized LicenseFactory getInstance() {
		if (instance == null) {
			instance = new LicenseFactory();
		}
		return instance;
	}
	
	public ILicense getLicense() {
		return DEFAULT_LICENSE;
	}
	
	public ILicense getLicense(Class LicenseClass) {
		if (LicenseClass == null) {
			return getLicense();
		} else {
			try {
				return (ILicense)LicenseClass.newInstance();
			} catch (InstantiationException e) {
				DaemonLogger.logError("License: " + LicenseClass.getName() + "Couldnt be instantiated!", e);
				return null;
			} catch (IllegalAccessException e) {
				DaemonLogger.logError(e);
				return null;
			}
		}
	}
	
	public ILicense getLicense(String className) {
		if (className == null || className.equals("")) {
			return getLicense();
		} else {
			Class clazz = null;
			try {
				clazz = Class.forName(className);
			} catch (ClassNotFoundException e) {
				DaemonLogger.logError("License Class: " + className + "Couldnt not be found!", e);
				return null;
			}
			return getLicense(clazz);
		}
	}
}
